package com.gosophia.conference.action.summary;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.commons.web.logAnnotation.LogRecord;
import com.gosophia.conferenceManagement.ConferenceTurningAction;
import com.gosophia.conferenceManagement.common.dto.DocumentDTO;
import com.gosophia.conferenceManagement.common.entity.Document;
import com.gosophia.conferenceManagement.common.entity.DocumentInfo;
import com.gosophia.conferenceManagement.common.service.ConferenceFileService;
import com.gosophia.conferenceManagement.summary.dto.SummaryJobDataDTO;
import com.gosophia.conferenceManagement.summary.ws.SummaryWebService;
import com.gosophia.oa.action.JudgeCurrentUndertaker;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.opensymphony.xwork2.ActionContext;

/**
 * 取回后提交审批
 * @author mayue
 *
 */
@Scope("prototype")
@Controller("GetBackSubmitApprovalAction")
public class GetBackSubmitApprovalAction extends BaseSupportAction{

    private static final long serialVersionUID = 1L;
    
    @Autowired
    private SummaryWebService summaryWebService;
    @Autowired
    private ConferenceFileService conferenceFileService;
    @Autowired
    private JudgeCurrentUndertaker judgeCurrentUndertaker;
    
    private SummaryJobDataDTO summaryJobDataDTO;

    //上传的文件描述
    private String[] filesNode;
    //上传的文件
    private File[] files;
    //文件名
    private String [] filesFileName;
    
    private List<Long> oldDocumentIds;
    
    private String onlinePath;
    
    private String onlineVersion;
    
    @Override
    @LogRecord(eventAction="会议管理-会议总结-取回并提交",eventTarget="summaryJobDataDTO")
    public String _execute() throws Exception {
        
        //判断当前用户和job承办人是否匹配,如果不匹配转到异常页面
        judgeCurrentUndertaker.judgeOperationAuthority(summaryJobDataDTO.getJobDTO().getJobId());
        
        UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
        .getContext().getAuthentication().getPrincipal();
        
        Long operatorId = userDetails.getUserAccountDTO().getUserInfoDTO()
        .getUserInfoId();
        
        
        //返回的附件赋值组装用于保存到数据库
        List<Document> returnDocuments = new ArrayList<Document>();
        List<DocumentDTO> oldDocumentList = new ArrayList<DocumentDTO>();
        List<DocumentDTO> returnDocDTO= new ArrayList<DocumentDTO>();
        //组装附件文件信息
        if(files!=null){
            for(int i=0 ; i<files.length ; i++){
                Document document = new Document();
                document.setDocumentName(filesFileName[i]);
                document.setDocumentNote(filesNode[i]);
                DocumentInfo documentInfo= new DocumentInfo();
                documentInfo.setDocumentBasicInfo(document);
                FileInputStream documentContent = new FileInputStream(files[i]);
                documentInfo.setDocumentContent(documentContent);
                //组装返回的附件信息
                Document returnDoc = conferenceFileService.saveDocumentData(documentInfo);
                returnDoc.setDocumentName(filesFileName[i]);
                returnDoc.setDocumentNote(filesNode[i]);
                returnDoc.setDocumentCategory("attachment");
                returnDocuments.add(returnDoc);
            }
            returnDocDTO = changeDocumentToDTO(returnDocuments);
        }
        oldDocumentList = summaryWebService.dealOldAttachment(oldDocumentIds);
        
        if(oldDocumentList!=null&&oldDocumentList.size()!=0){
            returnDocDTO.addAll(oldDocumentList);
        }
       //在线文档
        DocumentDTO onlineDocumentDTO = new DocumentDTO();
        onlineDocumentDTO.setDocumentPath(onlinePath);
        onlineDocumentDTO.setDocumentVersion(onlineVersion);
        onlineDocumentDTO.setDocumentCategory("online");
        
        summaryJobDataDTO.setAttachmentsDTOs(returnDocDTO);
        summaryJobDataDTO.setOnlineDocumentDTO(onlineDocumentDTO);
        
        summaryJobDataDTO.setTurningAction(ConferenceTurningAction.GETBACK_APPROVAL.getValue());
        summaryWebService.HandleConferenceTurningSummary(summaryJobDataDTO.getJobDTO().getJobId(), operatorId, summaryJobDataDTO);
        
        super.getRequestResult().setRsltCode("0");
        super.getRequestResult().setMessage("会议总结取回并提交审批成功");
        ActionContext.getContext().getSession().put("_lastOpResult", super.getRequestResult());
        
        if("task".equals(this.getFromFlag())){
            return "task";
        }
        return SUCCESS;
    }

    public SummaryJobDataDTO getSummaryJobDataDTO() {
        return summaryJobDataDTO;
    }

    public void setSummaryJobDataDTO(SummaryJobDataDTO summaryJobDataDTO) {
        this.summaryJobDataDTO = summaryJobDataDTO;
    }
    
    public String[] getFilesNode() {
        return filesNode;
    }

    public void setFilesNode(String[] filesNode) {
        this.filesNode = filesNode;
    }

    public File[] getFiles() {
        return files;
    }

    public void setFiles(File[] files) {
        this.files = files;
    }

    public String[] getFilesFileName() {
        return filesFileName;
    }

    public void setFilesFileName(String[] filesFileName) {
        this.filesFileName = filesFileName;
    }

    public List<Long> getOldDocumentIds() {
        return oldDocumentIds;
    }

    public void setOldDocumentIds(List<Long> oldDocumentIds) {
        this.oldDocumentIds = oldDocumentIds;
    }

    public String getOnlinePath() {
        return onlinePath;
    }

    public void setOnlinePath(String onlinePath) {
        this.onlinePath = onlinePath;
    }

    public String getOnlineVersion() {
        return onlineVersion;
    }

    public void setOnlineVersion(String onlineVersion) {
        this.onlineVersion = onlineVersion;
    }

    private List<DocumentDTO> changeDocumentToDTO(List<Document> oldDoc){
        List<DocumentDTO> newDoc = new ArrayList<DocumentDTO>();
        if(oldDoc!=null){
            for (Document document : oldDoc) {
                DocumentDTO d = new DocumentDTO();
                d.setDocumentCategory("attachement");
                d.setDocumentId(document.getDocumentId());
                d.setDocumentName(document.getDocumentName());
                d.setDocumentNote(document.getDocumentNote());
                d.setDocumentPath(document.getDocumentPath());
                d.setDocumentVersion(document.getDocumentVersion());
                newDoc.add(d);
            }
        }
        return newDoc;
    }


}
